const connection = require('../app/database');
const { APP_HOST, APP_PORT } = require('../app/config');

class FileService {

  // 判断是否有头像
  async isHaveAvatar(userId) {
    try {
      const statement = `SELECT * FROM avatar WHERE user_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0].length === 0 ? false : true;
    } catch (error) {
      console.log(error);
    }
  }

  // 判断是否有cover
  async isHaveCover(userId) {
    try {
      const statement = `SELECT * FROM cover WHERE user_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0].length === 0 ? false : true;
    } catch (error) {
      console.log(error);
    }
  }

  // 保存头像信息
  async saveAvatarInfo(filename, mimetype, size, userId) {
    try {
      if (await this.isHaveAvatar(userId)) {
        // 有头像了, 更新头像
        const statement = `UPDATE avatar SET filename = ?, mimetype = ?, size = ? WHERE user_id = ?`;
        const result = await connection.execute(statement, [filename, mimetype, size, userId]);
        return result[0];
      } else {
        // 没有头像
        const statement = `INSERT INTO avatar (filename, mimetype, size, user_id) VALUES (?, ?, ?, ?)`;
        const result = await connection.execute(statement, [filename, mimetype, size, userId]);
        return result[0];
      }
    } catch (error) {
      console.log(error);
    }
  }

  // 获取用户头像
  async getAvatarByUserId(userId) {
    try {
      const statement = `SELECT * FROM avatar WHERE user_id = ?`;
      const [result] = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error)
    }
  }

  // 保存cover图片
  async saveCoverInfo(filename, mimetype, size, userId) {
    try {
      if (await this.isHaveCover(userId)) {
        // 有cover了, 更新cover
        const statement = `UPDATE cover SET filename = ?, mimetype = ?, size = ? WHERE user_id = ?`;
        const result = await connection.execute(statement, [filename, mimetype, size, userId]);
        return result[0];
      } else {
        // 没有cover
        const statement = `INSERT INTO cover (filename, mimetype, size, user_id) VALUES (?, ?, ?, ?)`;
        const result = await connection.execute(statement, [filename, mimetype, size, userId]);
        return result[0];
      }
    } catch (error) {
      console.log(error);
    }
  }

  // 获取用户cover
  async getCoverByUserId(userId) {
    try {
      const statement = `SELECT * FROM cover WHERE user_id = ?`;
      const [result] = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error)
    }
  }

  // 保存动态配图
  async saveFile(filename, mimetype, size, userId, momentId) {
    try {
      const statement = `INSERT INTO file (filename, mimetype, size, user_id, moment_id) VALUES (?, ?, ?, ?, ?)`;
      const result = await connection.execute(statement, [filename, mimetype, size, userId, momentId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 查询动态配图
  async getFileByFilename(filename) {
    try {
      const statement = `SELECT * FROM file WHERE filename = ?`;
      const [result] = await connection.execute(statement, [filename]);
      return result[0];
    } catch (error) {
      console.log(error)
    }
  }

  // 查询用户壁纸
  async getWallByFilename(filename) {
    try {
      const statement = `SELECT mimetype FROM wall WHERE filename = ?`;
      const [result] = await connection.execute(statement, [filename]);
      return result[0];
    } catch (error) {
      console.log(error)
    }
  }

  // 保存用户壁纸
  async saveWallInfo(filename, mimetype, size, userId) {
    try {
      const statement = `INSERT INTO wall (filename, mimetype, size, user_id) VALUES (?, ?, ?, ?)`;
      const result = await connection.execute(statement, [filename, mimetype, size, userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 获取用户壁纸
  async getWallList(userId) {
    try {
      const statement = `SELECT 
      id,
      user_id userId,
      filename,
      CONCAT('${APP_HOST}:', '${APP_PORT}', '/wall/', filename) url,
      mimetype,
      size,
      createAt createTime,
      updateAt updateTime
      FROM wall
      WHERE user_id = ?`;
      const result = await connection.execute(statement, [userId])
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 删除用户壁纸
  async removeWall(wallId, userId) {
    try {
      const statement = `DELETE FROM wall WHERE id = ? AND user_id = ?`;
      const result = await connection.execute(statement, [wallId, userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 保存文章配图
  async saveArticlePicInfo(filename, mimetype, size, userId) {
    try {
      const statement = `INSERT INTO article_pic (filename, mimetype, size, user_id) VALUES (?, ?, ?, ?)`;
      const result = await connection.execute(statement, [filename, mimetype, size, userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 获取文章配图
  async getArticlePicByFilename(filename) {
    try {
      const statement = `SELECT mimetype FROM article_pic WHERE filename = ?`;
      const [result] = await connection.execute(statement, [filename]);
      return result[0];
    } catch (error) {
      console.log(error)
    }
  }

}

module.exports = new FileService();
